
package fileby.ctrl.sidebar;

import fileby.model.IFileManager;
import fileby.model.ISourceHandler;
import fileby.util.properties.IPropertyBundleProvider;
import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

/**
 *
 * @author Jacob
 */
public class SideBarItemListener extends MouseAdapter implements PopupMenuListener {

    private final static EmptyBorder EMPTY_BORDER = new EmptyBorder(new Insets(2,2,2,2));
    private final static EtchedBorder ETCHED_BORDER = new EtchedBorder();
    //This boolean is static to ensure that only one popup is visible
    private static boolean popupVisible;

    private Color previousColor;
    private IPropertyBundleProvider propertyProvider;
    private ISourceHandler sourceHandler;
    private IFileManager fileManager;

    public SideBarItemListener(IPropertyBundleProvider propertyProvider,
            ISourceHandler sourceHandler,
            IFileManager fileManager) {

        this.propertyProvider = propertyProvider;
        this.sourceHandler = sourceHandler;
        this.fileManager = fileManager;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        JPanel p = (JPanel)e.getSource();
        p.grabFocus();
        if (e.getButton() == MouseEvent.BUTTON1) {
            if (p.getName() != null) {
                File file = new File(p.getName());
                if (file.isDirectory()) {
                    sourceHandler.newSource(p.getName(), true);
                } else if (file.isFile()) {
                    fileManager.runFile(p.getName());
                }
            }
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        if (!popupVisible) {
            setRollover(e.getComponent(), true);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        if (!popupVisible) {
            setRollover(e.getComponent(), false);
        }
    }

    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        setRollover(((JPopupMenu)e.getSource()).getInvoker(), true);
        popupVisible = true;
    }

    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        JPanel p = (JPanel)((JPopupMenu)e.getSource()).getInvoker();
        p.setBackground(previousColor);
        p.setBorder(EMPTY_BORDER);
        popupVisible = false;
    }

    public void popupMenuCanceled(PopupMenuEvent e) {}

    private void setRollover(Component c, boolean rollover) {
        JPanel p = (JPanel)c;
        Color rolloverColor = propertyProvider.createGlobalBundle().getColor("selection.color");
        if (rollover) {
            if (!p.getBackground().equals(rolloverColor)) {
                previousColor = p.getBackground();
            }
            p.setBackground(rolloverColor);
            p.setBorder(ETCHED_BORDER);
        } else {
            p.setBackground(previousColor);
            p.setBorder(EMPTY_BORDER);
        }
    }

}
